﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DoAnB
{
    public partial class qlBanAn : Form
    {
        public qlBanAn()
        {
            InitializeComponent();
            loadBanAn();
            label1.Visible = false;
            textBox1.Visible = false;
            btnOK.Visible = false;
        }


        string sql = String.Empty;
        private void loadBanAn()
        {
            ketnoi data = new ketnoi();
            string sql = "Select ID, TenBan as 'Tên Bàn' From Ban";
            dataGridView1.DataSource = data.getDataTable(sql);
            dataGridView1.Columns[0].Visible = false;
        }


        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            textBox1.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            label1.Visible = false;
            textBox1.Visible = false;
            btnOK.Visible = false;
            ketnoi data = new ketnoi();
            if (sql == "Thêm")
            {
                sql = "Insert Into Ban(TenBan) Values('" + textBox1.Text + "')";
                data.ExeCuteNonQuery(sql);
            }
            if (sql == "Sữa")
            {
                
                sql = "Update Ban Set TenBan='" + textBox1.Text + "' Where ID='" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + "'";
                data.ExeCuteNonQuery(sql);
            }
           


            loadBanAn();

        }

        private void qlBanAn_Shown(object sender, EventArgs e)
        {
            dataGridView1.ClearSelection();
            dataGridView1.CurrentCell = null;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            label1.Visible = true;
            textBox1.Visible = true;
            btnOK.Visible = true;
            label1.Text = "Tên bàn mới:";
            sql = "Thêm";

        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentCell == null)
                MessageBox.Show("Chưa chọn bàn!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
            {
                textBox1.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                label1.Visible = true;
                textBox1.Visible = true;
                btnOK.Visible = true;
                label1.Text = "Tên cần sữa:";
                sql = "Sữa";

            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentCell == null)
                MessageBox.Show("Chưa chọn bàn!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
            {
                ketnoi data = new ketnoi();
                sql = "Delete From Ban Where ID=" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                data.ExeCuteNonQuery(sql);

                loadBanAn();

            }

        }

        private void qlBanAn_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult = DialogResult.OK;
            DialogResult r;
            r = MessageBox.Show("Bạn có muốn thoát không?", "Ex1", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
            if (r == DialogResult.No)
                e.Cancel = true;
           
        }
    }
}
